﻿








#region Designer generated code
using System;

using TrueCode.Core;
using TrueCode.Core.EditableEntity;
using Client.BL.Tests;

namespace Client.BL.Tests
{
	
	public partial class EditableEntity : IEditableEntity< Entity >, IDisposable
	{
		private readonly EditableEntity _parent;
		private readonly Entity _entity;
		private EditableObjectState _state;
		
				
		#region Original values
		private EditableObjectState _stateBeforeDeleted;

				#endregion
	
		#region .ctors
		public EditableEntity( Entity entity ) : this( entity, false ){}
		public EditableEntity( Entity entity, bool isNew )
		{
			if( entity == null ) throw new ArgumentNullException( "entity" );
			
			_entity = entity;
			if( !isNew )
				StoreOriginalValues();
			_state = isNew ? EditableObjectState.New : EditableObjectState.Original;
		}
		
		private EditableEntity( Entity entity, EditableEntity parent ) : this( entity )
		{
		  _parent = parent;
		}
		#endregion
	
		#region Entity Properties
				#endregion

		#region Implementation of IEditableEntity
		public Entity Entity
		{
			get{ return _entity; }
		}
		
		public EditableObjectState State
		{
			get { return _state; }
			private set
			{
				if( _state != value )
				{
					_state = value;
					PropertyChanged.Raise( this, () => State );
				}
			}
		}
		
		public void AcceptChanges()
		{
			if( _state == EditableObjectState.Deleted )
				throw new InvalidOperationException( "Can not accept changes of the deleted entity " );
			StoreOriginalValues();
			State = EditableObjectState.Original;
		}
		
		public void RejectChanges()
		{
			if( _state == EditableObjectState.New )
				throw new InvalidOperationException( "Can not reject changes of the New entity " );

			if( State == EditableObjectState.Deleted )
				State = _stateBeforeDeleted;
			else
			{
								
				PropertyChanged.Raise( this, null );
				State = EditableObjectState.Original;
			}
		}
		
		public void MarkDeleted()
		{
			if( _state == EditableObjectState.New )
				throw new InvalidOperationException( "Can not mark New entity as Deleted" );
			// If RejectChanges will be called, we should reject MarkDeleted change
			_stateBeforeDeleted = State;
			State = EditableObjectState.Deleted;

			OnMarkedDeleted();
		}
		
		public void UpdateAll( Entity entity )
		{
																		
				_entity.Id = entity.Id;
				
															
				_entity.Name = entity.Name;
				
															
				_entity.Revision = entity.Revision;
				
										
			SetAsModified();
			PropertyChanged.Raise( this, null );
		}
		
		#endregion
		
		#region Entry points
		public EditableEntity StartNewEditSession()
		{
			return new EditableEntity( _entity, this );
		}
		#endregion
		
		#region Protected
		protected void SetAsModified()
		{
			if( State == EditableObjectState.Deleted )
				throw new InvalidOperationException( "Can not modify deleted entity, use RejectChanges to undelete it" );

			if( State != EditableObjectState.New && State != EditableObjectState.Modified )
				State = EditableObjectState.Modified;
		}
		
		protected virtual void OnMarkedDeleted(){}
		#endregion

    	#region Private	
		private void StoreOriginalValues()
		{
					}
		#endregion
	
		#region IDisposable implementation
		public void Dispose()
		{
			if( State == EditableObjectState.Modified && _parent != null )
				_parent.UpdateAll( _entity );
		}
		#endregion
		
		#region Implementation of INotifyPropertyChanged
		public event System.ComponentModel.PropertyChangedEventHandler PropertyChanged;

		protected System.ComponentModel.PropertyChangedEventHandler PropertyChangedEvent
		{
		get{ return PropertyChanged; }
		}
		#endregion
	}
	
}

#endregion